package com.shop2cn.iapi.openapibiz.testcase;

import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.openapibiz.parameter.NewThirdPartyAppBean;
import com.shop2cn.iapi.openapibiz.service.NewThirdPartyAppCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.Application;
import com.ymttest.database.sqlwapper.OpenApiWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

/**
*创建第三方应用app
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_newThirdPartyApp {
	private static NewThirdPartyAppBean newthirdpartyappBean;
	private static NewThirdPartyAppCall newthirdpartyappCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("创建第三方应用app");
	}

	@Before
	public void caseUp() {
		newthirdpartyappBean = new NewThirdPartyAppBean();
		newthirdpartyappCall = new NewThirdPartyAppCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(newthirdpartyappCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_newThirdPartyApp_001() {
		Logger.start(true, "创建第三方应用");
		try {
			String appName = "appName_" + TestDataManager.getRandomNum(10);

			newthirdpartyappBean.setAppName(appName);
			newthirdpartyappBean.setAppDesc("dsfdfdf");
			newthirdpartyappCall.setData(newthirdpartyappBean);
			newthirdpartyappCall.callService();

			Logger.verifyEquals(ECode.SUCCESS, newthirdpartyappCall.getCode(),
					"验证Code");
			Logger.verifyEquals(appName, newthirdpartyappCall.getAppName(),
					"验证Code");

			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper.selectApplicationByAppName(
					appName).get(0);

			Logger.verifyEquals(newthirdpartyappCall.getAppId(),
					application.getApp_id(), "验证返回App_id与数据库一致");
			// 返回是原始数据，数据库是加密后的数据，需要查看源码中的加密解密算法
			//			Logger.verifyEquals(newthirdpartyappCall.getAppSecret(), application.getApp_secret(), "验证secret与数据库一致");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_newThirdPartyApp_002() {
		Logger.start(true, "appName不传");
		try {

			newthirdpartyappCall.setData(newthirdpartyappBean);
			newthirdpartyappCall.callService();

			Logger.verifyEquals("500", newthirdpartyappCall.getCode(), "验证Code");
			Logger.verifyEquals("处理失败,请稍后重试",
					newthirdpartyappCall.getMessage(), "验证Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_newThirdPartyApp_003() {
		Logger.start(true, "appName已存在");
		try {

			String appName = "appName_" + TestDataManager.getRandomNum(10);
			newthirdpartyappBean.setAppName(appName);

			newthirdpartyappCall.setData(newthirdpartyappBean);
			newthirdpartyappCall.callService();

			newthirdpartyappCall.setData(newthirdpartyappBean);
			newthirdpartyappCall.callService();

			Logger.verifyEquals("400", newthirdpartyappCall.getCode(), "验证Code");
			Logger.verifyEquals("参数错误", newthirdpartyappCall.getMessage(),
					"验证Code");

			OpenApiWapper openApiWapper = new OpenApiWapper();
			List<Application> applications = openApiWapper
					.selectApplicationByAppName(appName);

			Logger.verifyEquals(1, applications.size(), "验证返回App_id与数据库一致");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_newThirdPartyApp_004() {
		Logger.start(true, "appName含特殊字符");
		try {

			String appName = "appName_&%@!~sql*"
					+ TestDataManager.getRandomNum(10);
			newthirdpartyappBean.setAppName(appName);

			newthirdpartyappCall.setData(newthirdpartyappBean);
			newthirdpartyappCall.callService();

			Logger.verifyEquals(ECode.SUCCESS, newthirdpartyappCall.getCode(),
					"验证Code");
			Logger.verifyEquals(appName, newthirdpartyappCall.getAppName(),
					"验证Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}